package com.dzj.fishingcommunity.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dzj.fishingcommunity.common.ResponseResult;
import com.dzj.fishingcommunity.model.Order;
import com.dzj.fishingcommunity.model.User;
import com.dzj.fishingcommunity.service.OrderService;
import com.dzj.fishingcommunity.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orders")
@Tag(name = "订单管理", description = "订单管理相关的接口")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;

    @Operation(summary = "获取所有订单")
    @GetMapping
    public ResponseResult<List<Order>> getAllOrders(
            @Parameter(description = "当前页码", required = false) @RequestParam(defaultValue = "1") int current,
            @Parameter(description = "每页大小", required = false) @RequestParam(defaultValue = "10") int size) {
        Page<Order> page = new Page<>(current, size);
        Page<Order> orderPage = orderService.page(page);
        return ResponseResult.success(orderPage.getRecords());
    }

    @Operation(summary = "根据ID获取订单")
    @GetMapping("/{id}")
    public ResponseResult<Order> getOrderById(@PathVariable Long id) {
        Order order = orderService.getById(id);
        return ResponseResult.success(order);
    }

    @Operation(summary = "根据用户ID获取订单")
    @GetMapping("/user/{userId}")
    public ResponseResult<List<Order>> getOrdersByUserId(@PathVariable Long userId) {
        List<Order> orders = orderService.lambdaQuery().eq(Order::getUserId, userId).list();
        return ResponseResult.success(orders);
    }

    @Operation(summary = "新增订单")
    @PostMapping
    public ResponseResult<Order> addOrder(@RequestBody Order order) {

        //判断用户是否存在
        User user = userService.getById(order.getUserId());
        if(user == null){
            return ResponseResult.error(400, "用户不存在");
        }

        //判断库存是否足够
        boolean isStockEnough = orderService.isStockEnough(order);
        if(isStockEnough){
            orderService.save(order);
            return ResponseResult.success(order);
        }

        return ResponseResult.error(400, "库存不足");
    }

    @Operation(summary = "更新订单")
    @PutMapping("/{id}")
    public ResponseResult<Order> updateOrder(@PathVariable Long id, @RequestBody Order order) {
        order.setOrderId(id);
        orderService.updateById(order);
        return ResponseResult.success(order);
    }

    @Operation(summary = "删除订单")
    @DeleteMapping("/{id}")
    public ResponseResult<Void> deleteOrder(@PathVariable Long id) {
        Boolean isDeleted = orderService.deleteOrder(id);
        if(!isDeleted){
            //TODO 这里后续可以添加可以通知管理员的功能
            return ResponseResult.error(400, "订单删除出现异常");
        }
        return ResponseResult.success(null);
    }
}